<?php

include 'D:/dingjiangbbs/common/home.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);


//获取post值
var_dump($_POST);

$uid       = $_POST['uid'];
$new_pwd   = trim($_POST['newpassword']);
$clear     = $_POST['clear'];
$lock      = $_POST['lock'];
$email     = trim($_POST['email']);
$regip     = trim($_POST['regip']);
$regip     = ip2long($regip);
$regtime   = trim($_POST['regtime']);
$regtime   = strtotime("$regtime");
$lasttime  = trim($_POST['lasttime']);
$lasttime  = strtotime("$lasttime");
$num       = trim($_POST['grade']);

$autograph = trim($_POST['autograph']);
$realname  = trim($_POST['realname']);
$sex       = trim($_POST['sex']);
$place     = trim($_POST['place']);
$year      = $_POST['birthyear'];
$month     = $_POST['birthmonth'];
$day       = $_POST['birthday'];
$birthday  = strtotime("$year-$month-$day");
//var_dump($year,$month,$day);
//var_dump($birthday);
//var_dump(date('Y-m-d', 943891200));

//打印
//var_dump($new_pwd);
//var_dump($clear);
//var_dump($lock);
//var_dump($num);
//var_dump($email);
//var_dump($regip);
//var_dump($regtime);
//var_dump($lasttime );
//var_dump($autograph);
//var_dump($realname );
//var_dump($sex);
//var_dump($birthday);
//var_dump($place);
 

//查询用户所有原信息
$table   = DB_TABLE_USER;
$fields  = '*';
$where   = "where uid=$uid";
$res_all = select($link,$table,$fields,$where);
//var_dump($res_all);


//赋值给变量
$pwd        = $res_all[0]['password'];
$bcount     = $res_all[0]['bcount'];
$problem1   = $res_all[0]['problem'];
$result1    = $res_all[0]['result'];
$birthday1  = $res_all[0]['birthday'];
$sex1       = $res_all[0]['sex'];
$place1     = $res_all[0]['place'];
$autograph1 = $res_all[0]['autograph'];
//判断生日是否改动
if ($birthday == strtotime('0-0-0')) {

	$birthday = $birthday1;
}

//判断性别是否改动
if ($sex == 0) {

	$sex = $sex1;
} 


//判断籍贯是否改动
if (empty($place)) {

	$place = "$place1";
} 

//判断个性签名是否改动
if (empty($autograph)) {

	$autograph = "$autograph1";
} 

//积分操作
$grade   = $res_all[0]['grade'];
//var_dump($grade);

//判断分值是否超出已有分数
if ($grade + ($num) < 0) {

	exit('操作失败,该用户当前积分少于,你所减去的积分值');
}

$grade = $grade + ($num);
//var_dump($grade);


/*********判断新密码是否为空,并操作 ***********/
//若新密码不为空
if (!empty($new_pwd)) {

	//判断新密码长度
	if (strlen($new_pwd) > 12  ||  strlen($new_pwd) < 3) {
		exit('新密码长度未在3~12个字符之间');
	}
	//判断密码类型
	if (is_numeric($new_pwd)) {

		exit('新密码不能为纯数字');
	}

	if (md5($new_pwd) !== $pwd) {

		$new_pwd  = md5($new_pwd);
	}

}else{

	//若新密码为空
	$new_pwd  =  $pwd;
	//var_dump($new_pwd);
}


/***********锁定及清除安全问题*******/
//判断是否将锁定用户
if ($lock == 0) {

	$allowlogin = 0;
	$bcount    = 0;
}

if ($lock == 1) {

	$allowlogin = 1;

}

//判断是否清除安全问题
if ($clear == 1) {

	$problem = 'none';
	$result  = '空';
}

//判断邮箱格式
$ePattern = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
if (!preg_match($ePattern,$email)) {
	exit('邮箱格式错误');
}


/***********修改数据库字段值******************/
//修改对应的字段值
$data  = [
	
	'password'   => "$new_pwd",
	'allowlogin' => $allowlogin,
	'problem'    => "$problem",
	'result'     => "$result",

	'email'      => "$email",
	'regip'      => $regip,
	'regtime'    => $regtime,
	'lasttime'   => $lasttime,

	'grade'      => $grade,
	'autograph'  => "$autograph",
	'realname'   => "$realname",
	'sex'        => $sex,
	'place'      => $place,
	'birthday'   => "$birthday"
];

$table      = DB_TABLE_USER;
$where      = "uid=$uid";
$res_update = update($link,$table,$data,$where);
//var_dump($res_update);

if (!$res_update) {

	exit('修改失败');
}

//跳转页面
echo '修改成功<a href="'.WEB_SITE.'admin_cg.php?bighead=2&cg_number=31&id='.$uid.'">返回继续修改</a>';

mysqli_close($link);